/**
 * Copyright (c) <2010>, <SUNNY GUPTA>
 * All rights reserved.

 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the <ORGANIZATION> nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <SUNNY GUPTA> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.gwtp.investmentadvisor.server.entities.finance;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Currency;
import java.util.Date;
import java.util.List;

import javax.persistence.Embedded;
import javax.persistence.Id;

import com.googlecode.objectify.annotation.Unindexed;
import com.gwtp.investmentadvisor.server.externaldataservices.exception.CurrencyMismatchException;

/**
 * 
 * @author sunny
 *
 */
public class FinancialTransaction {
  @Id private Long id;
  @Embedded private List<FinancialEntry> entryList;
  @Unindexed private Currency currency;
  private Date date;
  
  /**
   * 
   */
  public FinancialTransaction() {
    // Initialize list
    this.entryList = new ArrayList<FinancialEntry>();
  }
  
  public boolean addFinancialEntry(FinancialEntry entry) throws CurrencyMismatchException {
    // Add entry and ensure same currency
    if (entryList.size() == 0) {
      entryList.add(entry);
      this.currency = entry.getCurrency();
      // Cannot be balanced, return false
      return false;
    } else {
      if (entry.getCurrency() != this.currency) {
        // Currency doesn't match
        throw new CurrencyMismatchException();
      }
      entryList.add(entry);
      return validateTransaction();
    }
  }
  
  public boolean removeFinancialEntry(FinancialEntry entry) throws CurrencyMismatchException {
    // Remove and validate
    entryList.remove(entry);
    return validateTransaction();
  }
  
  private boolean validateTransaction() throws CurrencyMismatchException {
    // Ensure that money is conserved in transaction
    FinancialAmount transactionSum = new FinancialAmount(this.currency);
    for (FinancialEntry partEntry : entryList) {
      transactionSum.add(partEntry.getResultantAmount());
    }
    
    /* TODO Think about checking the conservation of FinancialSecurity also. This will only
     * apply for transfer transaction so that the net quantity in transaction for each
     * FinancialSecurity is zero
     */
    
    return transactionSum.getValue().equals(BigDecimal.ZERO);
  }

  public void setDate(Date date) {
    this.date = date;
  }

  public Date getDate() {
    return date;
  }
}
